package com.logic.idevice.impl.kv7

import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.lifecycle.Lifecycle
import com.logic.idevice.IUHFSetting
import com.logic.view.Kv7EpcUserSwitchView
import com.orhanobut.hawk.Hawk
import com.uhf.api.cls.Reader
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import me.iso88591.uhfg.*
import me.iso88591.uhfg.common.UtilSound

class Kv7Setting(
    val loading: (Boolean) -> Unit,
    val refreshPowerUi: (Int) -> Unit,
    val refreshTemUi: (String) -> Unit,
) : IUHFSetting {

    companion object {

        var pow: Int
            get() = Hawk.get("Kv7Setting_pow") ?: 0
            set(value) {
                Hawk.put("Kv7Setting_pow", value)
            }


        enum class ScanAreaFlag {
            EPC, USER
        }

        //使用 epc标识
        var useEpcFlag: ScanAreaFlag
            get() = Hawk.get("Kv7Setting_useEPCFLAG") ?: ScanAreaFlag.USER
            set(value) {
                Hawk.put("Kv7Setting_useEPCFLAG", value)
            }


    }

    override fun init(context: Context, lifecycle: Lifecycle) {
        val useEpcOrUser = true
//        val lifecyUHFG = LifecycleUHFG(
//            UHFGConfig(
//                pow,
//                Reader.Region_Conf.RG_PRC,
//                false
//            ),
//            converter = IReadConverter { taginfo ->
//                //先用 epc 后用user
//                if (useEpcOrUser) {
//                    taginfo.epcStr(onAppend = ReadTagInfoParse.TwoDigitsOneSpace)
//                } else {
//                    taginfo.embStr(onAppend = ReadTagInfoParse.TwoDigitsOneSpace)
//                }
//            },
//        ) { uhfg, manager ->
//            if (manager != null) {
//                loading.invoke(false)
//
////                launch {
////                    uhfg.readerController.readData.collectLatest {
//////                        taglist.clear()
//////                        taglist.addAll(it)
////                    }
////                }
////
////                launch {
////                    uhfg.readerController.readStatusFlow.collect {
//////                        read = it
////                    }
////                }
//
//            }
//        }
//
//        lifecyUHFG.onInitSdk = {
//           loading.invoke(true)
//        }
//        lifecycle.addObserver(lifecyUHFG)

//        lifecycleOwner.apply {
//            keyCallBack(context) { isDown, it ->
//                if (KEY_CODE_TRIGGER_K71 == it) {
//                    if (isDown) {
//                        lifecyUHFG.readerController.loopRead(lifecycleScope)
//                    } else {
//                        lifecyUHFG.readerController.cancel()
//                    }
//                }
//            }
//        }
    }

    private lateinit var epcUserView: Kv7EpcUserSwitchView

    override fun specialUi(parent: FrameLayout) {
        super.specialUi(parent)

        epcUserView = Kv7EpcUserSwitchView(parent.context)
        parent.addView(
            epcUserView,
            ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        )

        //设置是否是 epc
        epcUserView.isEpc = Kv7Setting.useEpcFlag == Kv7Setting.Companion.ScanAreaFlag.EPC
        epcUserView.onEnsureState = {
            //自动保存配置
            useEpcFlag = if (epcUserView.isEpc) ScanAreaFlag.EPC else ScanAreaFlag.USER
        }
        epcUserView.onClickChange = {
            if (epcUserView.isEpc == it) {
                epcUserView.isEpc = it
            } else {
                AlertDialog.Builder(parent.context)
                    .setNegativeButton("取消", DialogInterface.OnClickListener { dialog, which ->
                        epcUserView.isEpc = epcUserView.isEpc
                        dialog.dismiss()
                    }).setPositiveButton("确定", DialogInterface.OnClickListener { dialog, which ->
                        epcUserView.isEpc = it
                        dialog.dismiss()
                    }).setTitle("是否修改默认读写区域为${if (it) "EPC" else "USER"}")
                    .setCancelable(false)
                    .create()
                    .show()
            }

        }

    }

    override fun openVoince(): Boolean {
        return UtilSound.soundEnable
    }

    override fun save(power: Int, openVoince: Boolean) {
        //保存配置
        pow = power
        UtilSound.soundEnable = openVoince

        useEpcFlag = if (epcUserView.isEpc) ScanAreaFlag.EPC else ScanAreaFlag.USER
    }

    override fun readTemperature(): String {
        return ""
    }

    override fun refreshTemperature() {
        refreshTemUi(readTemperature())
    }

    override fun readPower(): Int {
        return pow
    }

    override fun refreshPower() {
        refreshPowerUi(readPower())
    }
}